﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace HappyChinese
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        List<Chinese> chineses = new List<Chinese>();
        private void button1_Click_1(object sender, EventArgs e)
        {
            Thread thread = new Thread(delegate ()
              {
                  Chinese chinese = new Chinese(textBox1.Text);
                  listBox1.Items.Add(chinese);
                  chineses.Add(chinese);
                  chinese.MsgChanged += Chinese_MsgChanged;
                  chinese.Start();
              });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }

        private void Chinese_MsgChanged(object sender, string msg)
        {
            listBox1.Items[listBox1.Items.IndexOf(sender)] = sender;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (var ln in richTextBox1.Lines)
            {
                Thread thread = new Thread(delegate ()
                {
                    Chinese chinese = new Chinese(ln);
                    listBox1.Items.Add(chinese);
                    chineses.Add(chinese);
                    chinese.MsgChanged += Chinese_MsgChanged;
                    chinese.Start();
                });
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
            }
        }
    }
}
